<?php 

namespace Weixin\Model;

use Common\Model\Model;

class TeachModel extends Model
{
    
    public function getTeachCount($catid = 0)
    {
        $where['status'] = 1;
        if($catid)
        {
            $where['catid'] = $catid;
        }
        $count = M('Teachlist')->where($where)->count();
        return $count;
    }
    
/**
     * 获取导师列表
     * @param unknown $userid
     * @param unknown $first
     * @param unknown $last
     * @return Ambigous <unknown, string>
     */
    public function getTeachList($catid = 0, $first, $last)
    {
        $where = '';
        if($catid)
        {
            $where .= ' and T.catid='.$catid;
        }
        $Model=new Model();
        $list=$Model
        ->table(array(
            C('DB_PREFIX').'user'=>'U',
            C('DB_PREFIX').'teachlist'=>'T'
        )
        )
        ->where('U.userid=T.userid and T.status=1'.$where)
        ->field('T.teachid, T.title, T.content, T.create_time, T.type , T.iflight, T.ifimg, T.msgnum, U.nickname')
        ->order('T.create_time DESC')
        ->limit($first . ',' . $last)
        ->select();
        if($list)
        {
            foreach($list as $k => $v)
            {
//                 echo $list[$k]['type'];
                $list[$k]['create_time'] = format_time($v['create_time'], 'Y-m-d H:i');
                $list[$k]['title'] = hy_substr($v['title'], 10);
//                 $nickname = M('User')->where(array('userid' => $v['userid']))->getField('nickname');
//                 $list[$k]['nickname'] = hy_substr($nickname, 8);
                $list[$k]['nickname'] = hy_substr($v['nickname'], 10);
                if($v['ifimg'])
                {
                    preg_match('/<\s*img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', htmlspecialchars_decode($v['content']), $match);
                    if($match[0])
                    {
                        $list[$k]['image'] = $match[2];
                    }
                }
                $list[$k]['content'] = hy_substr(strip_tags(htmlspecialchars_decode($v['content'])), 30);
            }
        }
        return $list;
    }
    
    public function getTeachCategory()
    {
        $category = M('Teachcate')->where(array('status' => 1))->order('sort desc')->select();
        return $category;
    }
    
    public function getTeachAd($position, $limit = 1)
    {
        $ad = M('Teachad')->where(array('status' => 1, 'position' => $position))->order('sort desc')->limit($limit)->select();
        return $ad;
    }
    
    public function getTeachInfo($teachid)
    {
        $info = M('Teachlist')->where(array('teachid' => $teachid))->find();
        if($info)
        {
            $user = M('User')->where(array('userid' => $info['userid']))->field('nickname, uavatar')->find();
            $info['nickname'] = $user['nickname'];
            $info['uavatar'] = $user['uavatar'];
            $info['create_time'] = format_time($info['create_time'], 'Y-m-d H:i');
            $info['content2'] = $info['content'] = htmlspecialchars_decode($info['content']);
            $content = $info['content'];
            preg_match_all('/<\s*img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', htmlspecialchars_decode($content), $match);
            $tempImg = $match[0];
            $info['image'] = $match[2];
            if($info['image'])
            {
                foreach($info['image'] as $k => $v)
                {
                    $info['image'][$k] = WEB_URL.$v;
                }
            }
            $temp = preg_replace('/<\s*p><\s*img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*><\/p>/i', '', $content);
            $info['content'] = preg_replace('/<\s*img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', '', $content);
            $info['all_pic'] = implode(',', $info['image']);
            $info['title'] = hy_substr($info['title'], 15, true);
        }
        return $info;
    }
    
}